package com.mindether.llyblog.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mindether.llyblog.dao.ArticleMapper;
import com.mindether.llyblog.pojo.ApiPageResponse;
import com.mindether.llyblog.pojo.Article;
import com.mindether.llyblog.pojo.Comment;
import com.mindether.llyblog.utils.IdWorker;
import com.mindether.llyblog.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IdWorker idWorker;
    public Article getArticleById(String id){
        return articleMapper.getArticleById(id);
    }

    public Article recommendArticle(){
        return articleMapper.getRecommendArticle();
    }

    @Transactional
    public boolean addArticle( Article article){
        articleMapper.add(article);
        return true;
    }

    @Transactional
    public boolean updateArticle( Article article){
        articleMapper.update(article);
        return true;
    }

    @Transactional
    public boolean disableArticle( String id){
        articleMapper.disableArticle(id);
        return true;
    }
    @Transactional
    public boolean deleteById( String id){
        articleMapper.delete(id);
        return true;
    }

    /**
     * 分页查询所有的文章
     * @param page
     * @param size
     * @return
     */
    public ApiPageResponse<Article> findArticleByPage(Integer page, Integer size){
        Page<Article> articlePage = PageHelper.startPage(page, size, true);
        List<Article> articles=articleMapper.getAllArticleByPage();
        return Utils.<Article>pageUtil(articles,articlePage);
    }

    /**
     * 查询某个标签下所有的文章
     * @param page
     * @param size
     * @param tag
     * @return
     */
    public ApiPageResponse<Article> findArticleByTagAndPage(Integer page, Integer size,String tag){
        Page<Article> articlePage = PageHelper.startPage(page, size, true);
        List<Article> articles=articleMapper.getArticleListByTag(tag);
        return Utils.<Article>pageUtil(articles,articlePage);
    }

    public List<Article> findArticleListByTag(String tag){
        return articleMapper.getArticleListByTag(tag);
    }

    public List<Article>  getTopCommentArticleList(){
        return articleMapper.getTopCommentArticleList();
    }


    public List<Article>  allArticleList(){
        return articleMapper.getAllArticle();
    }


    public  boolean increaseArticleComment(String id){
        articleMapper.increaseArticleComment(id);
        return true;
    }
    @Transactional
    public  boolean increaseArticleVisit(String id){
        articleMapper.increaseArticleVisit(id);
        return true;
    }

    /**
     * 获取某个某个下相关的文章
     * @param tag
     * @return
     */
    public List<Article> findRelateArticle(String tag) {
        Page<Article> articlePage = PageHelper.startPage(1, 6, true);
        List<Article> relateArticles=articleMapper.getArticleListByTag(tag);
        return relateArticles;
    }
}

